<?php

namespace App\Http\Controllers;

use App\Models\OrderInfo;
use App\Models\TrafficDetail;
use App\Services\ProxyService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class RecordController extends Controller
{
    protected $proxyService;

    public function __construct(ProxyService $proxyService)
    {
        $this->proxyService = $proxyService;
    }

    /**
     * 流量使用记录
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function traffic(Request $request)
    {
        $user_id = Auth::id();
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $offset = ($page - 1) * $limit;

        $query = TrafficDetail::where('user_id', $user_id);
        $count = $query->count();
        $list = $query->offset($offset)->limit($limit)->orderBy('create_day', 'desc')->get();
        foreach ($list as $item) {
            $item->total = bcadd($item->download_flow, $item->upload_flow, 2);
        }
        return response()->json([
            'errcode' => 0,
            'errmsg' => __('common.success'),
            'data' => [
                'count' => $count,
                'list' => $list
            ]
        ]);
    }

    /**
     * 动态代理订单
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function dynamicOrder(Request $request)
    {
        $user_id = auth()->id();
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $offset = ($page - 1) * $limit;

        if (App::getLocale() == 'en') {
            $goods_list = config('goods.dynamic.en');
        } else {
            $goods_list = config('goods.dynamic.zh-cn');
        }

        $field = ['order_nu', 'goods_id', 'goods_info', 'amount', 'pay_way', 'status', 'created_at'];
        $query = OrderInfo::select($field)->where('user_id', $user_id)->where('proxy_type', 1);
        $count = $query->count();
        $list = $query->offset($offset)->limit($limit)->orderBy('created_at', 'desc')->get();
        foreach ($list as $item) {
            $goods_config = $goods_list[$item->goods_id] ?? [];
            if ($goods_config) {
                $item->goods_info = $goods_config['title'] . ' ' . $goods_config['combo'];
            }
            if (App::getLocale() == 'en') {
                if ($item->pay_way == 'usdt') {
                    $item->pay_way_str = 'USDT';
                } elseif ($item->pay_way == 'alipay') {
                    $item->pay_way_str = 'Alipay';
                } elseif ($item->pay_way == 'card') {
                    $item->pay_way_str = 'Credit Card';
                } else {
                    $item->pay_way_str = 'other';
                }
                if ($item->status == 1) {
                    $item->status_str = 'have paid';
                } elseif ($item->status == 2) {
                    $item->status_str = 'refunded';
                } else {
                    $item->status_str = 'non-payment';
                }
            } else {
                if ($item->pay_way == 'usdt') {
                    $item->pay_way_str = 'USDT';
                } elseif ($item->pay_way == 'alipay') {
                    $item->pay_way_str = '支付宝';
                } elseif ($item->pay_way == 'card') {
                    $item->pay_way_str = '信用卡';
                } else {
                    $item->pay_way_str = '其他';
                }
                if ($item->status == 1) {
                    $item->status_str = '已支付';
                } elseif ($item->status == 2) {
                    $item->status_str = '已退款';
                } else {
                    $item->status_str = '未支付';
                }
            }
        }
        return response()->json([
            'errcode' => 0,
            'errmsg' => __('common.success'),
            'data' => [
                'count' => $count,
                'list' => $list
            ]
        ]);
    }

    /**
     * 静态代理订单
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function staticOrder(Request $request)
    {
        $user_id = auth()->id();
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $offset = ($page - 1) * $limit;

        $field = ['order_nu', 'goods_id', 'goods_info', 'pay_way', 'amount', 'created_at', 'status', 'is_renew'];
        $query = OrderInfo::select($field)->where('user_id', $user_id)->where('proxy_type', 2);
        $count = $query->count();
        $list = $query->offset($offset)->limit($limit)->orderBy('created_at', 'desc')->get();
        foreach ($list as $item) {
            if (App::getLocale() == 'en') {
                if ($item->pay_way == 'usdt') {
                    $item->pay_way_str = 'USDT';
                } elseif ($item->pay_way == 'alipay') {
                    $item->pay_way_str = 'Alipay';
                } elseif ($item->pay_way == 'card') {
                    $item->pay_way_str = 'Credit Card';
                } else {
                    $item->pay_way_str = 'other';
                }
                if ($item->status == 1) {
                    $item->status_str = 'have paid';
                } elseif ($item->status == 2) {
                    $item->status_str = 'refunded';
                } else {
                    $item->status_str = 'non-payment';
                }
            } else {
                if ($item->pay_way == 'usdt') {
                    $item->pay_way_str = 'USDT';
                } elseif ($item->pay_way == 'alipay') {
                    $item->pay_way_str = '支付宝';
                } elseif ($item->pay_way == 'card') {
                    $item->pay_way_str = '信用卡';
                } else {
                    $item->pay_way_str = '其他';
                }
                if ($item->status == 1) {
                    $item->status_str = '已支付';
                } elseif ($item->status == 2) {
                    $item->status_str = '已退款';
                } else {
                    $item->status_str = '未支付';
                }
            }
        }
        return response()->json([
            'errcode' => 0,
            'errmsg' => __('common.success'),
            'data' => [
                'count' => $count,
                'list' => $list
            ]
        ]);
    }
}
